/**
 * @brief
 *
 * @file    aiio_common.c
 * @copyright Copyright (C) 2020-2023, Shenzhen Anxinke Technology Co., Ltd
 * @note
 * @par Change Logs:
 * <table>
 * <tr><th>Date               <th>Version             <th>Author           <th>Notes
 * <tr><td>2023-07-14          <td>1.0.0            <td>zhuolm             <td>
 */

#include "aiio_common.h"
#include "aiio_adapter_include.h"


static bool aiio_buffer_is_empty_check(char* buffer, uint32_t buffer_len)
{
    if (buffer == NULL)
    {
        aiio_log_e("param err \r\n");
        return false;
    }

    for (uint32_t num = 0; num < buffer_len; num++)
    {
        if (buffer[num] != 0xff)
        {
            return false;
        }
    }

    return true;
}


static bool aiio_wifi_config_data_is_empty_check(wifi_config_data_t* config_data)
{
    if (config_data == NULL)
    {
        aiio_log_e("param err \r\n");
        return true;
    }

    if (aiio_buffer_is_empty_check(config_data->ssid, sizeof(config_data->ssid)))               return true;
    if (aiio_buffer_is_empty_check(config_data->password, sizeof(config_data->password)))             return true;
    if (aiio_buffer_is_empty_check(config_data->wificc, sizeof(config_data->wificc)))             return true;
    if (aiio_buffer_is_empty_check(config_data->mqttip, sizeof(config_data->mqttip)))             return true;
    if (aiio_buffer_is_empty_check(config_data->token, sizeof(config_data->token)))              return true;
    if (aiio_buffer_is_empty_check(config_data->tz, sizeof(config_data->tz)))                 return true;
    if (config_data->port == 0xff)                                   return true;


    if (aiio_strlen(config_data->ssid) == 0  ||
        aiio_strlen(config_data->wificc) == 0 || aiio_strlen(config_data->mqttip) == 0 ||
        aiio_strlen(config_data->token) == 0 || aiio_strlen(config_data->tz) == 0 || config_data->port == 0)
    {
        return true;
    }

    return false;
}


int aiio_flash_get_wifi_config_data(wifi_config_data_t* config_data)
{
    uint16_t wifi_config_data_len = 0;
    int32_t ret = 0;

    if (config_data == NULL)
    {
        aiio_log_e("param err ");
        return AIIO_ERROR;
    }

    aiio_log_d("aiio_flash_read ");
    wifi_config_data_len = sizeof(wifi_config_data_t);
    aiio_flash_read(FLASH_RW_WIFI_DATA_ADDR, config_data, &wifi_config_data_len);

    aiio_log_d("aiio_wifi_config_data_is_empty_check ");
    if (aiio_wifi_config_data_is_empty_check(config_data))
    {
        aiio_log_e("Failed to get the data from the flash");
        return 0;
    }

    aiio_log_d("ssid = %s ", config_data->ssid);
    aiio_log_d("password = %s", config_data->password);
    aiio_log_d("wificc = %s", config_data->wificc);
    aiio_log_d("mqttip = %s ", config_data->mqttip);
    aiio_log_d("token = %s", config_data->token);
    aiio_log_d("tz = %s ", config_data->tz);
    aiio_log_d("ts = %d ", config_data->ts);
    aiio_log_d("port = %d", config_data->port);

    aiio_log_d("wifi_config_data_len = %d", wifi_config_data_len);

    return wifi_config_data_len;
}

void aiio_flash_save_wifi_config_data(wifi_config_data_t* config_data)
{
    aiio_flash_erase(FLASH_RW_WIFI_DATA_ADDR, sizeof(wifi_config_data_t));
    aiio_flash_write(FLASH_RW_WIFI_DATA_ADDR, config_data, sizeof(wifi_config_data_t));
    aiio_log_d("wifi config data save ok \r\n");
}


void aiio_flash_clear_config_data(void)
{
    int32_t ret = 0;
    wifi_config_data_t config_data = { 0 };

    aiio_flash_erase(FLASH_RW_WIFI_DATA_ADDR, sizeof(wifi_config_data_t));
    aiio_flash_write(FLASH_RW_WIFI_DATA_ADDR, &config_data, sizeof(wifi_config_data_t));
    aiio_log_d("nvs set data ok \r\n");

}

